package view;

import java.util.ArrayList;

import view.widget.HotspotNotificationBannerManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.shilehui.www.R;

import core.ServerHelper;
import core.data.HelpDataInfomation;

public class MainLayerActivity extends Activity {
	private HotspotNotificationBannerManager hotspotNotificationBannerManager = null;

	/*
	 * class ImageAdapter is used to control gallery source and operation.
	 */
	private static class ImageAdapter extends BaseAdapter implements
			OnItemSelectedListener {

		private Context context;
		private ArrayList<HelpDataInfomation> hotspostBuffer;
		private HotspotNotificationBannerManager hotspotNotificationBannerManager = null;

		public ImageAdapter(Context context,
				HotspotNotificationBannerManager manager) {
			this.context = context;
			this.hotspostBuffer = ServerHelper.sharedServerHelper()
					.getHotsposts();
			this.hotspotNotificationBannerManager = manager;
		}

		@Override
		public int getCount() {
			return Integer.MAX_VALUE;
		}

		@Override
		public Object getItem(int position) {
			int index = position % hotspostBuffer.size();
			return hotspostBuffer.get(index);
		}

		@Override
		public long getItemId(int position) {
			return position % hotspostBuffer.size();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view =  LayoutInflater.from(context).inflate(R.layout.gallery_item_layout,
						null);
			ImageView imageView = (ImageView) view.findViewById(R.id.galler_item_image_view);
			imageView.setImageDrawable(hotspostBuffer.get(
					position % hotspostBuffer.size()).getImage());
			imageView.setFadingEdgeLength(0);
			imageView.setFocusable(false);
			return view;
		}

		@Override
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			hotspotNotificationBannerManager.setCurrentIndex((int) id);
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.main_layer);

		LinearLayout list = (LinearLayout) findViewById(R.id.infoList);
		ArrayList<HelpDataInfomation> listBuffer = ServerHelper
				.sharedServerHelper().getHelpList();
		for (HelpDataInfomation infomation : listBuffer) {
			View view = LayoutInflater.from(this).inflate(R.layout.listitem,
					null);
			ImageView image = (ImageView) view.findViewById(R.id.ItemImage);
			TextView title = (TextView) view.findViewById(R.id.ItemTitle);

			image.setImageDrawable(infomation.getImage());
			String titleData = infomation.getTitle();
			title.setText(titleData);
			view.setClickable(true);
			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					Toast.makeText(MainLayerActivity.this, "Click",
							Toast.LENGTH_LONG).show();
				}
			});
			list.addView(view);
		}

		FrameLayout galleryFrameLayout = (FrameLayout) findViewById(R.id.gallery_frame_layout);
		hotspotNotificationBannerManager = new HotspotNotificationBannerManager(
				this, listBuffer);
		FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
				FrameLayout.LayoutParams.WRAP_CONTENT,
				FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
		galleryFrameLayout.addView(
				hotspotNotificationBannerManager.getHotspotBanner(),
				layoutParams);

		Gallery hotspot = (Gallery) findViewById(R.id.hotspot);
		ImageAdapter adapter = new ImageAdapter(this,
				hotspotNotificationBannerManager);
		hotspot.setAdapter(adapter);
		hotspot.setOnItemSelectedListener(adapter);
		hotspot.setSelection((Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE / 2) % listBuffer.size());
		hotspot.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				Toast.makeText(MainLayerActivity.this, "" + id,
						Toast.LENGTH_SHORT).show();
			}
		});

		ImageButton quitBtn = (ImageButton) findViewById(R.id.quit_btn);
		quitBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				System.exit(0);
			}
		});
		
		ImageButton loginBtn = (ImageButton) findViewById(R.id.login_btn);
		loginBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(MainLayerActivity.this,
						LoginActivity.class);
				MainLayerActivity.this.startActivity(intent);
			}
		});
	}

}
